package com.mf.morefun.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.mf.morefun.R;
import com.mf.morefun.listener.MyRequestListener;
import com.mf.morefun.listener.ReadDetailTypeListener;
import com.mf.morefun.widget.GlideRoundTransform;
import com.mf.morefun.widget.TransformationUtils;
import com.zhcx.zhcxlibrary.utils.DeviceUtils;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TestListenSelectPicAdapter extends RecyclerView.Adapter<TestListenSelectPicAdapter.ViewHolder> {
    private Context mContext;
    private List<String> imageUrl;
    private ReadDetailTypeListener listener;

    public ReadDetailTypeListener getListener() {
        return listener;
    }

    public void setListener(ReadDetailTypeListener listener) {
        this.listener = listener;
    }

    public TestListenSelectPicAdapter(Context mContext, List<String> imageUrl) {
        this.mContext = mContext;
        this.imageUrl = imageUrl;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_listenselectpic, null));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (null != imageUrl && imageUrl.size() > 0){
//            holder.view.setTag(position);
            holder.view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.callBack(position);
                }
            });
            RequestOptions options = new RequestOptions().transform(new GlideRoundTransform(mContext, 14));
            Glide.with(mContext).asBitmap().load(imageUrl.get(position)).listener(new MyRequestListener(holder.ivImg, mContext, DeviceUtils.dip2px(mContext, 158), DeviceUtils.dip2px(mContext, 130))).apply(options).into(new com.mf.morefun.util.TransformationUtils(holder.ivImg, mContext, DeviceUtils.dip2px(mContext, 158), DeviceUtils.dip2px(mContext, 130)));
        }
    }

    @Override
    public int getItemCount() {
        return imageUrl.size();
    }

   public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivImg;
        View view;
        public ViewHolder(View itemView) {
            super(itemView);
            ivImg = itemView.findViewById(R.id.iv_img);
            this.view = itemView;
        }
    }
}
